import time

import pygame
from pygame.locals import *
import sys

import image
import chessman

pygame.init()
GAME_SIZE = (800, 800)
left_top = (65, 15)
right_bottom = (780, 730)
# 218 165
# 3 3
jiange = 50

# x-32,y-32
# 设置棋盘大小
DISPLAYSURF = pygame.display.set_mode(GAME_SIZE)
# 设置背景颜色
DISPLAYSURF.fill((255, 255, 255))
# 设置背景图
# board_img = pygame.image.load('./pic/board.png')
board_img = image.Image('./pic/board.png', (0, 0), (800, 800))
black_img = chessman.Chessman('./pic/black.png', (0, 0), (64, 64))
white_img = chessman.Chessman('./pic/white.png', (100, 0), (64, 64))
# pygame.transform.scale(board_img, GAME_SIZE)

win_img = image.Image('./pic/win.png', (0, 200), (800, 400))
lose_img = image.Image('./pic/lose.png', (0, 200), (600, 300))

board_img.update()
board_img.draw(DISPLAYSURF)

pygame.display.update()
black = True
# 点击事件
list_x = [65, 115, 167, 217, 268, 319, 371, 420, 472, 523, 574, 626, 677, 729, 780]
list_y = [14, 64, 116, 167, 217, 269, 319, 373, 425, 474, 527, 578, 628, 681, 731]

list_all = []
list_black = []
list_white = []

finish = False


def find_closest_value(target, lst):
    min_diff = float('inf')
    index = None

    for i, val in enumerate(lst):
        diff = abs(target - val)
        if diff < min_diff:
            min_diff = diff
            # closest_value = val
            index = i
    return index


def check_win(zb, single_list):
    if len(single_list) < 5:
        return False
    # 获取最后一次下子的坐标
    x, y = zb

    # 检查水平方向
    count = 0
    for piece in single_list:
        if piece[0] == x and abs(piece[1] - y) < 5:
            count += 1
    if count >= 5:
        return True

    # 检查垂直方向
    count = 0
    for piece in single_list:
        if piece[1] == y and abs(piece[0] - x) < 5:
            count += 1
    if count >= 5:
        return True

    # 检查主对角线方向（右上到左下）
    count = 0
    for piece in single_list:
        if abs(piece[0] - x) == abs(piece[1] - y) and abs(piece[0] - x) < 5:
            count += 1
    if count >= 5:
        return True

    # 检查副对角线方向（左上到右下）
    count = 0
    for piece in single_list:
        if abs(piece[0] - x) == abs(piece[1] - y) and abs(piece[0] - x) < 5:
            count += 1
    if count >= 5:
        return True

    return False


while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if finish:
                continue
            # 鼠标左键
            if event.button != 1:
                continue
            # 鼠标点击位置
            x, y = pygame.mouse.get_pos()
            xx = find_closest_value(x, list_x)
            yy = find_closest_value(y, list_y)

            # 坐标
            zb = (xx, yy)
            if zb in list_all:
                continue
            if black:
                if zb in list_black:
                    continue
                black_img = chessman.Chessman('./pic/black.png', (list_x[xx] - 32, list_y[yy] - 32), (64, 64))
                black_img.update()
                black_img.draw(DISPLAYSURF)
                black = False
                list_black.append(zb)
                if check_win(zb, list_black):
                    print("黑子获胜")
                    finish = True
                    win_img.update()
                    win_img.draw(DISPLAYSURF)
                else:
                    print("还未决出胜负")
            else:
                if zb in list_white:
                    continue
                white_img = chessman.Chessman('./pic/white.png', (list_x[xx] - 32, list_y[yy] - 32), (64, 64))
                white_img.update()
                white_img.draw(DISPLAYSURF)
                black = True
                list_white.append(zb)
                if check_win(zb, list_white):
                    print("白子获胜")
                    finish = True
                    win_img.update()
                    win_img.draw(DISPLAYSURF)
                else:
                    print("还未决出胜负")
            # 判断是否胜利
            pygame.display.update()
